package com.ozdemo.annoioc.service.impl;

import com.ozdemo.annoioc.annotation.Autowired;
import com.ozdemo.annoioc.annotation.Service;
import com.ozdemo.annoioc.annotation.Transactional;
import com.ozdemo.annoioc.dao.AccountDao;
import com.ozdemo.annoioc.pojo.Account;

/**
 * 交易实现
 */
@Service("transferService1")
public class TransferServiceImpl1{

    // 最佳状态
    // @Autowired 按照类型注入 ,如果按照类型无法唯一锁定对象，可以结合@Qualifier指定具体的id
    @Autowired
    private AccountDao accountDao;

    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(to);
        //System.out.println(1/0);
        accountDao.updateAccountByCardNo(from);
    }
}
